package com.atguigu.day03;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * @author Felix
 * @date 2024/7/10
 * 该案例演示了自定义数据源
 */
public class Flink08_Source_Cus {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set(RestOptions.PORT,8088);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);

        DataStreamSource<String> ds = env.addSource(
                new MySource()
        );
        //env.fromSource();
        ds.print();
        env.execute();
    }
}

class MySource implements SourceFunction<String>{
    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        for (int i = 0; i < 100000; i++) {
             //将数据向下游传递
            ctx.collect("数据" + i);
            Thread.sleep(1000);
        }
    }

    @Override
    public void cancel() {
        System.out.println("~~~job被取消了~~~");
    }
}
